package com.hailian.fintech.hlfinanceserver.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hailian.fintech.hlcomm_interfaceserver.dto.BaoliDto;
import com.hailian.fintech.hlcomm_interfaceserver.entity.POJO;
import com.hailian.fintech.hlcomm_interfaceserver.util.ExcelBean;
import com.hailian.fintech.hlcomm_interfaceserver.util.ExcelUtils;
import com.hailian.fintech.hlfinanceserver.entity.Baoli;
import com.hailian.fintech.hlfinanceserver.mapper.BaoliMapper;
import com.hailian.fintech.hlfinanceserver.service.IBaoliService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.IntrospectionException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wk
 * @since 2020-05-23
 */
@Service
public class BaoliServiceImpl extends ServiceImpl<BaoliMapper, Baoli> implements IBaoliService {

    @Autowired
    private BaoliMapper baoliMapper;
    @Override
    public PageInfo selectAllOwner(Integer pageNum, Integer pageSize, POJO pojo) {
        PageHelper.startPage(pageNum,pageSize);
        List<Baoli> list = baoliMapper.selectAllOwner(pojo);
        list.forEach(baoli ->{
            int price = baoli.getPay() * baoli.getPrice().intValue() + baoli.getDetain() * baoli.getCashPledge().intValue();
            baoli.setTotalprice(new BigDecimal(price));//求总价格
            if(baoli.getGatherBefore() != null){
                Integer a = baoli.getEndTime().getDate();
                baoli.getEndTime().setDate(a-baoli.getGatherBefore());
                baoli.setJiaozu(baoli.getEndTime());
            }else{
                baoli.getEndTime().setDate(baoli.getGatherFix());
                baoli.setJiaozu(baoli.getEndTime());
            }
        });
        return new PageInfo(list);
    }

    @Override
    public XSSFWorkbook downLoadGoodsExcel(BaoliDto baoliDto) throws InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {

        PageHelper.startPage(baoliDto.getPageNo(),baoliDto.getPageSize());
        List<Baoli> list = baoliMapper.selectAllOwner(new POJO());
        list.forEach(baoli ->{
            int price = baoli.getPay() * baoli.getPrice().intValue() + baoli.getDetain() * baoli.getCashPledge().intValue();
            baoli.setTotalprice(new BigDecimal(price));//求总价格
            if(baoli.getGatherBefore() != null){
                Integer a = baoli.getEndTime().getDate();
                baoli.getEndTime().setDate(a-baoli.getGatherBefore());
                baoli.setJiaozu(baoli.getEndTime());
            }else{
                baoli.getEndTime().setDate(baoli.getGatherFix());
                baoli.setJiaozu(baoli.getEndTime());
            }
        });
        PageInfo pageInfo = new PageInfo(list);
        Map<Integer, List<ExcelBean>> map = new HashMap<Integer, List<ExcelBean>>();

        List<ExcelBean> excelBeans = new ArrayList<>();
        excelBeans.add(new ExcelBean("id","id",0));
        excelBeans.add(new ExcelBean("申请合同号","number",0));
        excelBeans.add(new ExcelBean("公寓名称","houseId",0));
        excelBeans.add(new ExcelBean("承租房间","houseId",0));
        excelBeans.add(new ExcelBean("租客姓名","consumerName",0));
        excelBeans.add(new ExcelBean("应还保理租金","totalprice",0));
        excelBeans.add(new ExcelBean("账期","created",0));
        excelBeans.add(new ExcelBean("交租日期","endTime",0));
        //excelBeans.add(new ExcelBean("状态","stateStr",0));
        excelBeans.add(new ExcelBean("资金方","endTime",0));
        map.put(0,excelBeans);
        XSSFWorkbook book = ExcelUtils.createExcelFile(Baoli.class, pageInfo.getList(), map, "商品列表");
        return book;
    }

    @Override
    public void uploadExcel(InputStream inputStream, String originalFilename) throws Exception {
        List<List<Object>> bankListByExcel = ExcelUtils.getBankListByExcel(inputStream, originalFilename);
        for (List<Object> objects : bankListByExcel) {
            Baoli baoli = new Baoli();

            baoli.setNumber(String.valueOf(objects.get(1)));
            baoli.setConsumerId(Integer.valueOf(objects.get(2).toString()));
          /*  baoli.setInventoryQuantity(Integer.valueOf(objects.get(3).toString()));
            baoli.setPurchasingPrice(Float.valueOf(objects.get(4).toString()));
            baoli.setSellingPrice(Float.valueOf(objects.get(5).toString()));
            baoli.setUnit(objects.get(6).toString());
            baoli.setLastPurchasingPrice(Float.valueOf(objects.get(7).toString()));
            baoli.setState(2);
            baoli.setModel(objects.get(8).toString());
            baoli.setMinNum(100);*/
          baoliMapper.insert(baoli);
        }
    }
}
